﻿using BaBaoFan.Modifier.Model;
using System;
using System.Collections.Generic;
using System.Text;
using Dapper;
using System.Data;
using AutoMapper;
using System.Linq;

namespace BaBaoFan.Modifier.Data
{
    public class PoolRepository : IPoolRepository
    {
        private IDbConnection _conn;
        public PoolRepository(DbConnection conn)
        {
            _conn = conn.Connection;
        }

        public bool Add(PoolInfo poolInfo)
        {
            string sql = @"insert into pool_info
(poolname,poollogo,poolarea,poolpaytime,pooladdress,poolstate,sort,pooldescription,keywords,poolscore,addtime,isvisable)
values 
(@PoolName,@PoolLogo,@PoolArea,@PoolPayTime,@PoolAddress,@PoolState,@Sort,@PoolDescription,@KeyWords,@PoolScore,@AddTime,@IsVisable)";
            return _conn.Execute(sql, poolInfo) > 0;
        }

        public bool Delete(int[] ids)
        {
            string sql = "delete from pool_info where id in @Ids";
            string deletePoolCoinSql = "delete from pool_coin where poolid in @Ids";
            _conn.Execute(deletePoolCoinSql, new { Ids = ids });
            return _conn.Execute(sql, new { Ids=ids }) > 0;
        }

        public PoolInfo GetPoolInfoById(int id)
        {
            string sql = @"select 
                                    p.id as Id,
                                    p.poolname as PoolName,
                                    p.poollogo as PoolLogo,
                                    p.poolarea as PoolArea,
                                    p.poolpaytime as PoolPayTime,
                                    p.pooladdress as PoolAddress,
                                    p.poolstate as PoolState,
                                    p.sort as Sort,
                                    p.pooldescription as PoolDescription,
                                    p.keywords as KeyWords,
                                    p.poolscore as PoolScore,
                                    p.addtime as AddTime,
                                    p.isvisable as IsVisable
                                    from pool_info as p 
                                    where p.id=@Id";

            return _conn.QueryFirstOrDefault<PoolInfo>(sql, new { Id = id });
        }

        public IEnumerable<PoolArea> GetPoolAreas()
        {
            string sql = "select id as Id,countryname as CountryName from source_area";
            return _conn.Query<PoolArea>(sql);
        }
        public IEnumerable<PoolInfoDto> GetPoolInfos()
        {
            string sql = @"select 
                                    p.id as Id,
                                    p.poolname as PoolName,
                                    p.poollogo as PoolLogo,
                                    s.countryname as PoolArea,
                                    p.poolpaytime as PoolPayTime,
                                    p.pooladdress as PoolAddress,
                                    poolstate as PoolState,
                                    p.sort as Sort,
                                    p.pooldescription as PoolDescription,
                                    p.keywords as KeyWords,
                                    p.poolscore as PoolScore,
                                    p.addtime as AddTime,
                                    p.isvisable as IsVisable,
                                    c.id as Id,
                                    c.coin_name as CoinName,
                                    c.coin_device as CoinDevice,
                                    c.coin_short as CoinShort,
                                    c.coin_time as CoinTime,
                                    c.coin_unit as  CoinUnit,
                                    c.coin_weight as CoinWeight,
                                    c.coin_isexcavate as CoinIsExcavate,
                                    c.coin_sort as CoinSort

                                    from pool_info as p
                                    left join source_area as s on s.id=p.poolarea
                                    left join pool_coin as pc on pc.poolid=p.id
                                    left join coin_type as c on c.id=pc.coinid";
            var lookUp = new Dictionary<int, PoolInfoDto>();
            try
            {
                _conn.Query<PoolInfoDto, CoinInfo, PoolInfoDto>(sql, (p, c) =>
                  {
                      PoolInfoDto poolInfo = new PoolInfoDto();
                      if (!lookUp.TryGetValue(p.Id, out poolInfo))
                      {
                          poolInfo = p;
                          lookUp.Add(poolInfo.Id, poolInfo);
                      }
                      poolInfo.CoinInfos.Add(c);
                      return poolInfo;
                  });
                return lookUp.Values;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public bool Update(PoolInfo poolInfo)
        {
            string sql = @"update  pool_info set
                                    poolname=@PoolName,
                                    poollogo=@PoolLogo,
                                    poolarea=@PoolArea,
                                    poolpaytime=@PoolPayTime,
                                    pooladdress=@PoolAddress,
                                    poolstate=@PoolState,
                                    sort=@Sort,
                                    pooldescription=@PoolDescription,
                                    keywords=@KeyWords,
                                    poolscore=@PoolScore,
                                    addtime=@AddTime,
                                    isvisable=@IsVisable
                                    where id=@Id";
            return _conn.Execute(sql, poolInfo) > 0;
        }

        public bool AddPoolCoins(IEnumerable<PoolCoin> poolCoins)
        {
            string sql = "insert into pool_coin (poolid,coinid,minpay,coinstate,mergedmining,date) values (@PoolId,@CoinId,@MinPay,@CoinState,@MergedMining,@Date)";
            return _conn.Execute(sql, poolCoins) > 0;
        }

        public bool AddPoolCoin(PoolCoin poolCoin)
        {
            string sql = "insert into pool_coin (poolid,coinid,minpay,coinstate,mergedmining,date) values (@PoolId,@CoinId,@MinPay,@CoinState,@MergedMining,@Date)";
            return _conn.Execute(sql, poolCoin) > 0;
        }
    }
}
